﻿//-----------------------------------------------------------------------------
//<filedescription file="CustomMessageBox.cs" company="Microsoft">
//  <copyright>
//     ***  DESIGNATED SAMPLE CODE (per the included software license terms) ***
//      THIS CODE SAMPLE IS MADE AVAILABLE TO YOU WITHOUT WARRANTY OF ANY KIND
//      AND CONFERS NO RIGHTS ON YOU WHATSOEVER EXCEPT THE RIGHT TO REVIEW IT
//      SOLELY AS A REFERENCE.  THE ENTIRE RISK OF USE OR RESULTS FROM USE OF
//      THIS CODE SAMPLE REMAINS WITH YOU.
//
//      NOTE: THIS CODE *ADAPTED* FROM SAMPLE SOURCE CODE AVAILABLE FROM
//            CODEPROJECT.COM UNDER THE TITLE "CUSTOM FEATURED MESSAGEBOX".
//            THIS CODE FREELY AVAILABLE, DISTRIBUTABLE, MODIFYABLE THROUGH
//            THE CODE PROJECT OPEN LICENSE, AVAILABLE AT:
//              http://www.codeproject.com/info/cpol10.aspx
//
//            NO COPYRIGHT OR LICENSING INFORMATION WAS INCLUDED IN THE
//            AUTHOR'S ORIGINAL SOURCE CODE, SO NO REQUIREMENT TO REPRODUCE
//            THE COPYRIGHT INFORMATION EXISTS.
//
//  </copyright>
//  <purpose>
//     Creates a dialog similar to a MessageBox. 
//  </purpose>
//  <notes>
//  </notes>
//</filedescription>
//-----------------------------------------------------------------------------



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Microsoft.ConfigurationManagement.IntegrationDevelopmentKit   
{

    /// <summary>
    ///     
    /// </summary>
    /// <remarks>
    ///     
    /// </remarks>
    public partial class XMessageBox : Form
    {
        XMessageBox()
        {
            InitializeComponent();
        }
        const int minWidth = 270;
        const int maxWidth = 600;


        void XmessageBox(string text, string title, Size size, Font font, Color backColor, Color foreColor, bool customSize)
        {
            int width;
            int height;

            this.Text = title;
            textBox1.Text = text;
            this.Font = font;
            this.BackColor = backColor;
            this.ForeColor = foreColor;

            Graphics g = this.CreateGraphics();
            SizeF textSize = g.MeasureString(text, font);
            g.Dispose();
            //|==================================================//
            //|------------                                      //
            //|CALCULATIONS                                      //
            //|------------                                      //            
            //|15, adjustment                                    //
            //|==================================================//            
            if (textSize.Width <= maxWidth)
                width = (int)textSize.Width + 15;
            else
                width = maxWidth;

            //|==================================================//
            //|------------                                      //
            //|CALCULATIONS                                      //
            //|------------                                      //
            //|30 + 10 = 40, panal height + form's top padding   //            
            //|40, adjustment                                    //
            //|==================================================//
            height = (textBox1.LinesCount() * textBox1.Font.Height) + 40 + 40;

            if (customSize == true)  // set size only when customSize is true
                this.Size = size;
            else
                this.Size = new Size(width, height);
        }

        int GetButtonLocation(int buttonId, int totalButtons)
        {
            int buttonSize = 75;
            int padding = 6;

            int totalButtonWidth = (totalButtons * buttonSize) + ((totalButtons - 1) * padding);

            //             [center of form]  [half of width of buttons]   [add space for buttons to left, including padding]
            int location = ((this.Width / 2) - (totalButtonWidth / 2) + ((buttonId - 1) * buttonSize) + ((buttonId - 1) * padding)) ;
 
            // Example: 1 button
            // (400 / 2 = 200) - (75 / 2 = 37) + (1-1 = 0) * 75 = 0) + (1-1=0) * 6 = 0) = 163
            
            // Example: 2 buttons
            // (400 / 2 = 200) - (156 / 2 = 78) + (1-1 = 0) * 75 = 0)  + (1-1=0) * 6 = 0) = 200 - 78 = 122
            // (400 / 2 = 200) - (156 / 2 = 78) + (2-1 = 1) * 75 = 75) + (2-1=1) * 6 = 6) = 200 - 78 + 81 = 203
            
            // Example: 3 buttons
            // (400 / 2 = 200) - (237 / 2 = 118) + (1-1 = 0) * 75 = 0)   + (1-1=0) * 6 = 0)  = 200 - 118 = 82
            // (400 / 2 = 200) - (237 / 2 = 118) + (2-1 = 1) * 75 = 75)  + (2-1=1) * 6 = 6)  = 200 - 118 + 81 = 199
            // (400 / 2 = 200) - (237 / 2 = 118) + (3-1 = 1) * 75 = 150) + (3-1=1) * 6 = 12) = 200 - 118 + 162 = 280
            
            return location;
        }

        void SetButtons(MessageBoxButtons buttons)
        {
            if (buttons == MessageBoxButtons.OK)
            {
                button2.Visible = true;
                button2.Text = "OK";
                button2.DialogResult = DialogResult.OK;
                button2.Location = new Point(GetButtonLocation(1, 1));
                button2.Focus();
                ControlBox = true;
            }
            else if (buttons == MessageBoxButtons.AbortRetryIgnore)
            {
                button1.Visible = true;
                button1.Text = "Abort";
                button1.DialogResult = DialogResult.Abort;
                button1.Location = new Point(GetButtonLocation(1, 3));
                button2.Visible = true;
                button2.Text = "Retry";
                button2.DialogResult = DialogResult.Retry;
                button2.Location = new Point(GetButtonLocation(2, 3));
                button3.Visible = true;
                button3.Text = "Ignore";
                button3.DialogResult = DialogResult.Ignore;
                button3.Location = new Point(GetButtonLocation(3, 3));
                
                if (this.Width < minWidth)
                    this.Width = minWidth;
            }
            else if (buttons == MessageBoxButtons.RetryCancel)
            {
                button1.Visible = true;
                button1.Text = "Retry";
                button1.DialogResult = DialogResult.Retry;
                button1.Location = new Point(GetButtonLocation(1, 2));
                button2.Visible = true;
                button2.Text = "Cancel";
                button2.DialogResult = DialogResult.Cancel;
                button2.Location = new Point(GetButtonLocation(2, 2));
                if (this.Width < minWidth)
                    this.Width = minWidth;
            }
            else if (buttons == MessageBoxButtons.YesNo)
            {
                button1.Visible = true;
                button1.Text = "Yes";
                button1.DialogResult = DialogResult.Yes;
                button1.Location = new Point(GetButtonLocation(1, 2));
                button2.Visible = true;
                button2.Text = "No";
                button2.DialogResult = DialogResult.No;
                button2.Location = new Point(GetButtonLocation(2, 2));
                if (this.Width < minWidth)
                    this.Width = minWidth;
            }
            else if (buttons == MessageBoxButtons.YesNoCancel)
            {
                button1.Visible = true;
                button1.DialogResult = DialogResult.Yes;
                button1.Text = "Yes";
                button1.Location = new Point(GetButtonLocation(1, 3));
                button2.Visible = true;
                button2.DialogResult = DialogResult.No;
                button2.Text = "No";
                button2.Location = new Point(GetButtonLocation(2, 3));
                button3.Visible = true;
                button3.DialogResult = DialogResult.Cancel;
                button3.Text = "Cancel";
                button3.Location = new Point(GetButtonLocation(3, 3));
                if (this.Width < minWidth)
                    this.Width = minWidth;
            }
        }

        void SetDefaultButton(MessageBoxDefaultButton button)
        {
            if (button == MessageBoxDefaultButton.Button1)
            {
                button1.Focus();
            }
            else if (button == MessageBoxDefaultButton.Button2)
            {
                button2.Focus();
            }
            else
            {
                button3.Focus();
            }
        }

        void SetIcon(MessageBoxIcon icon)
        {
            if (this.Height < 104)
                this.Height = 104;
            if (this.Width <= 600)
                this.Width += 55;
            leftPanel.Visible = true;
            switch (icon)
            {
                // NOTE: The statements commented out are actually because the numeric value they represent
                //       are duplicated in other MessageBoxIcon values

                //case MessageBoxIcon.Asterisk:
                //    pictureBox1.BackgroundImage = Bitmap.FromHicon(SystemIcons.Asterisk.Handle);
                //    break;

                case MessageBoxIcon.Error:
                    pictureBox1.BackgroundImage = Bitmap.FromHicon(SystemIcons.Error.Handle);
                    break;

                //case MessageBoxIcon.Exclamation:
                //    pictureBox1.BackgroundImage = Bitmap.FromHicon(SystemIcons.Exclamation.Handle);
                //    break;

                //case MessageBoxIcon.Hand:
                //    pictureBox1.BackgroundImage = Bitmap.FromHicon(SystemIcons.Hand.Handle);
                //    break;

                case MessageBoxIcon.Information:
                    pictureBox1.BackgroundImage = Bitmap.FromHicon(SystemIcons.Information.Handle);
                    break;

                case MessageBoxIcon.None:
                    break;

                case MessageBoxIcon.Question:
                    pictureBox1.BackgroundImage = Bitmap.FromHicon(SystemIcons.Question.Handle);
                    break;

                //case MessageBoxIcon.Stop:
                //    pictureBox1.BackgroundImage = Bitmap.FromHicon(SystemIcons.Hand.Handle);
                //    break;

                case MessageBoxIcon.Warning:
                    pictureBox1.BackgroundImage = Bitmap.FromHicon(SystemIcons.Warning.Handle);
                    break;

                default:
                    break;

            }
            
        }



        /// <summary>
        ///     
        /// </summary>
        /// <param name="text" type="string">
        ///     <para>
        ///         
        ///     </para>
        /// </param>
        /// <returns>
        ///     A System.Windows.Forms.DialogResult value...
        /// </returns>
        public static DialogResult ShowMessageBox(string text)
        {
            XMessageBox f = new XMessageBox();
            f.XmessageBox(text, "", f.Size, f.Font, f.BackColor, f.ForeColor, false);
            f.SetButtons(MessageBoxButtons.OK); // if button is not specified
            return f.ShowDialog();
        }

        /// <summary>
        ///     
        /// </summary>
        /// <param name="text" type="string">
        ///     <para>
        ///         
        ///     </para>
        /// </param>
        /// <param name="title" type="string">
        ///     <para>
        ///         
        ///     </para>
        /// </param>
        /// <returns>
        ///     A System.Windows.Forms.DialogResult value...
        /// </returns>
        public static DialogResult Show(string text, string title)
        {
            XMessageBox f = new XMessageBox();
            f.XmessageBox(text, title, f.Size, f.Font, f.BackColor, f.ForeColor, false);
            f.SetIcon(MessageBoxIcon.Warning);
            f.SetButtons(MessageBoxButtons.OK); // if button is not specified
            return f.ShowDialog();
        }

        /// <summary>
        ///     
        /// </summary>
        /// <param name="text" type="string">
        ///     <para>
        ///         
        ///     </para>
        /// </param>
        /// <param name="title" type="string">
        ///     <para>
        ///         
        ///     </para>
        /// </param>
        /// <param name="buttons" type="Microsoft.ConfigurationManagement.IntegrationDevelopmentKit.MessageBoxButtons">
        ///     <para>
        ///         
        ///     </para>
        /// </param>
        /// <returns>
        ///     A System.Windows.Forms.DialogResult value...
        /// </returns>
        public static DialogResult Show(string text, string title, MessageBoxButtons buttons)
        {
            XMessageBox f = new XMessageBox();
            f.XmessageBox(text, title, f.Size, f.Font, f.BackColor, f.ForeColor, false);
            f.SetIcon(MessageBoxIcon.Warning); 
            f.SetButtons(buttons);
            return f.ShowDialog();
        }

        /// <summary>
        ///     
        /// </summary>
        /// <param name="text" type="string">
        ///     <para>
        ///         
        ///     </para>
        /// </param>
        /// <param name="title" type="string">
        ///     <para>
        ///         
        ///     </para>
        /// </param>
        /// <param name="buttons" type="Microsoft.ConfigurationManagement.IntegrationDevelopmentKit.MessageBoxButtons">
        ///     <para>
        ///         
        ///     </para>
        /// </param>
        /// <param name="icon" type="Microsoft.ConfigurationManagement.IntegrationDevelopmentKit.MessageBoxIcon">
        ///     <para>
        ///         
        ///     </para>
        /// </param>
        /// <returns>
        ///     A System.Windows.Forms.DialogResult value...
        /// </returns>
        public static DialogResult Show(string text, string title, MessageBoxButtons buttons, MessageBoxIcon icon)
        {
            XMessageBox f = new XMessageBox();
            f.XmessageBox(text, title, f.Size, f.Font, f.BackColor, f.ForeColor, false);
            f.SetButtons(buttons);
            f.SetIcon(icon);
            return f.ShowDialog();
        }

        /// <summary>
        ///     
        /// </summary>
        /// <param name="text" type="string">
        ///     <para>
        ///         
        ///     </para>
        /// </param>
        /// <param name="title" type="string">
        ///     <para>
        ///         
        ///     </para>
        /// </param>
        /// <param name="buttons" type="System.Windows.Forms.MessageBoxButtons">
        ///     <para>
        ///         
        ///     </para>
        /// </param>
        /// <param name="icon" type="System.Windows.Forms.MessageBoxIcon">
        ///     <para>
        ///         
        ///     </para>
        /// </param>
        /// <param name="defaultButton" type="System.Windows.Forms.MessageBoxDefaultButton">
        ///     <para>
        ///         
        ///     </para>
        /// </param>
        /// <returns>
        ///     A System.Windows.Forms.DialogResult value...
        /// </returns>
        public static DialogResult Show(string text, string title, MessageBoxButtons buttons, MessageBoxIcon icon, MessageBoxDefaultButton defaultButton)
        {
            XMessageBox f = new XMessageBox();
            f.XmessageBox(text, title, f.Size, f.Font, f.BackColor, f.ForeColor, false);
            f.SetButtons(buttons);
            f.SetDefaultButton(defaultButton);
            f.SetIcon(icon);
            return f.ShowDialog();
        }

        private void XMessageBox_Resize(object sender, EventArgs e)
        {
            int x = (bottomPanel.Width - bottomInnerPanel.Width) / 2;
            bottomInnerPanel.Location = new Point(x, bottomInnerPanel.Location.Y);
        }
        private void XMessageBox_ForeColorChanged(object sender, EventArgs e)
        {
            textBox1.ForeColor = this.ForeColor;
        }
        private void XMessageBox_BackColorChanged(object sender, EventArgs e)
        {
            textBox1.BackColor = this.BackColor;
        }  


    }
       

    /// <summary>
    ///     
    /// </summary>
    /// <remarks>
    ///     
    /// </remarks>
    public class XTextBox : TextBox
    {
        /// <summary>
        ///     
        /// </summary>
        /// <returns>
        ///     A int value...
        /// </returns>
        public int LinesCount()
        {
            Message msg = Message.Create(this.Handle, 0xBA, IntPtr.Zero, IntPtr.Zero);
            base.DefWndProc(ref msg);
            return msg.Result.ToInt32();
        }
    }

}
